import request from '@/utils/http';
import { jsonToUrlParam, getApiUrl } from '../../urlSearchParams';
import type { GetIdentityRolesInput, IdentityRoleDto, RoleCreateDto, RoleUpdateDto } from './type';
import type { PagedResultDto } from '../type';

// Api地址
enum Api {
  DEFAULT_URL = 'api/basics/roles',
  ROLE_ALL_URL = 'api/basics/roles/all',
  ROLE_PERMISSION_URL = 'api/basics/roles/permissions'
}

// 列表接口
export const getListAsync = (input: GetIdentityRolesInput) => {
  const paramenter = jsonToUrlParam(input);
  const url = getApiUrl(Api.DEFAULT_URL, paramenter);
  return request.get<PagedResultDto<IdentityRoleDto>>(url);
};

// 角色权限
export const getPermissionListAsync = (id: string) =>
  request.get<Array<string>>(`${Api.ROLE_PERMISSION_URL}/${id}`);

// 详情接口
export const getAsync = (id: string) => request.get<IdentityRoleDto>(`${Api.DEFAULT_URL}/${id}`);

// 创建
export const createAsync = (input: RoleCreateDto) =>
  request.post<IdentityRoleDto>(Api.DEFAULT_URL, input);

// 更新
export const updateAsync = (id: string, input: RoleUpdateDto) =>
  request.put<IdentityRoleDto>(`${Api.DEFAULT_URL}/${id}`, input);

// 删除
export const deleteAsync = (id: string) => request.delete<void>(`${Api.DEFAULT_URL}/${id}`);

// 全部角色
export const getAllAsync = () => request.get<string[]>(Api.ROLE_ALL_URL);
